const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const getConfig = require('../../utils/configUtil').getConfig;
const config = getConfig.dataBase.ctcDb;
const sequelize = new Sequelize(config.database, config.username, config.password, config);
const db = {};

fs
  .readdirSync(__dirname)
  .filter(file => (/\.\w+?$/.exec(file)[0] === '.js') && (file !== 'index.js'))
  .forEach((file) => {
    const model = sequelize.import(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach((modelName) => {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db);
  }
});

db.Ctcdb = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
